package org.aeon.client.handler.Inbound;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
import org.aeon.client.context.GlobalContext;
import org.aeon.message.response.RegisterResponseMessage;

import static org.aeon.client.context.MessageMap.MessageContextKey.RegisterMessage;


/**
 * @author: MissingAeon
 * @date: 2024/1/27 12:54
 * @description:
 */
@Slf4j
@ChannelHandler.Sharable
public class RegisterResponseHandler extends SimpleChannelInboundHandler<RegisterResponseMessage> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, RegisterResponseMessage msg) throws Exception {
        GlobalContext.getContext().getMessageMap().set(RegisterMessage, msg.getReason());
        final GlobalContext context = GlobalContext.getContext();
        if (msg.isSuccess()) {
            // 注册成功
            context.getIsLogin().set(true);
            context.setCurrentUser(msg.getUser());
        }
        ctx.fireChannelRead(msg);
    }
}
